
window.onload=function () {

    let currentPage=0;
    let allpage=0;
    document.getElementById("search").addEventListener("click",()=>{

        let value=document.getElementById("searchinput").value;
        if(value!=""){

         //发送请求查询

            if (XMLHttpRequest) {
                xhr = new XMLHttpRequest();
            }

            let url="/searcher?tag="+value;
            xhr.open("GET",url,true);
            xhr.send();
            xhr.onload=function () {

                var tgd = JSON.parse(xhr.responseText).data;
                console.log(tgd);
                if(tgd.length>=1){


                    var list=document.getElementById("list");
                    console.log(list.children.length);
                    for(let j=1;j<list.children.length;j++){

                        list.removeChild(list.children[j]);
                    }
                    var tgd = JSON.parse(xhr.responseText).data;
                    let target=document.getElementById("infobox");
                    console.log(target);
                    target.style.opacity=1;
                    let innertarget=  target.children[0].children[0];
                    innertarget.children[0].innerHTML="公司名称: "+tgd[0].companyName;
                    innertarget.children[1].innerHTML="公司联系人: "+tgd[0].peopleName;
                    innertarget.children[2].innerHTML="联系电话: "+tgd[0].companyPhone;
                    target.children[0].children[1].children[0].src=tgd[0].imgurl;

                    for(let i=1;i<tgd.length;i++){

                        console.log("ooo");
                        let node=target.cloneNode(true);
                        let tn =  node.children[0].children[0];
                        tn.children[0].innerHTML="公司名称: "+tgd[i].companyName;
                        tn.children[1].innerHTML="公司联系人: "+tgd[i].peopleName;
                        tn.children[2].innerHTML="联系电话: "+tgd[i].companyPhone;
                        node.children[0].children[1].children[0].src=tgd[i].imgurl;
                        list.appendChild(node);
                    }

                }else {

                    var list=document.getElementById("list");
                    console.log(list.children.length);
                    for(let j=1;j<list.children.length;j++){

                        list.removeChild(list.children[j]);
                    }

                }
            }
        }else {

            initView();

        }
    });



    initView();
    getCount();
    //初始化界面
    function initView() {


        if (XMLHttpRequest) {
            xhr = new XMLHttpRequest();
        }
        let url="/getcompanyinfo?page="+currentPage;
        xhr.open("GET",url,true);
        xhr.send();
        xhr.onload=function () {

         var list=document.getElementById("list");

         for(let j=1;j<list.children.length;j++){

             list.removeChild(list.children[j]);
         }
         var tgd = JSON.parse(xhr.responseText).data;
         console.log(tgd);
         let target=document.getElementById("infobox");
         console.log(target);
         target.style.opacity=1;
         let innertarget=  target.children[0].children[0];
         innertarget.children[0].innerHTML="公司名称: "+tgd[0].companyName;
         innertarget.children[1].innerHTML="公司联系人: "+tgd[0].peopleName;
         innertarget.children[2].innerHTML="联系电话: "+tgd[0].companyPhone;
         target.children[0].children[1].children[0].src=tgd[0].imgurl;

         for(let i=1;i<tgd.length;i++){

             console.log("ooo");
             let node=target.cloneNode(true);
             let tn =  node.children[0].children[0];
             tn.children[0].innerHTML="公司名称: "+tgd[i].companyName;
             tn.children[1].innerHTML="公司联系人: "+tgd[i].peopleName;
             tn.children[2].innerHTML="联系电话: "+tgd[i].companyPhone;
             node.children[0].children[1].children[0].src=tgd[i].imgurl;
             list.appendChild(node);
         }

        }
    }


    document.getElementById("before").addEventListener("click",()=>{
       //上一页

        if(currentPage>1){
            currentPage--;
            initView();

        }

    });


    document.getElementById("next").addEventListener("click",()=>{
        //下一页

        console.log(allpage);
    if(currentPage<allpage){

        currentPage++;
        initView();
    }

    });


    function getCount() {


        setTimeout(()=>{
            let xh4=null;
            if (XMLHttpRequest) {
                xhr = new XMLHttpRequest();
            }
            let url="/getPage";
            xhr.open("GET",url,true);
            xhr.send();
            xhr.onload=function () {

                var tgd = JSON.parse(xhr.responseText).status;
                allpage=tgd/4;
            }

        },2000);


    }


}